import {
  CallHandler,
  ExecutionContext,
  Injectable,
  Logger,
  NestInterceptor,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
  private log = new Logger(LoggingInterceptor.name);
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const req = context.switchToHttp().getRequest();
    const method = req.method;
    const url = req.url;
    const body = req.body;
    const params = req.params;
    const query = req.query;
    this.log.verbose(`${method} ${url}`);
    this.log.verbose(`req query: ${JSON.stringify(query)}`);
    this.log.verbose(`req body: ${JSON.stringify(body)}`);

    return next.handle().pipe(
      tap((data) => {
        this.log.verbose(`res Data: ${JSON.stringify(data)}`);
      }),
    );
  }
}
